#!/usr/bin/env python
# coding=utf-8

if __name__ == '__main__':
    """ for file debug"""
    import sys,os
    sys.path.insert(0,os.path.join( os.getcwd(), '..' ))

import http.server
import vavava.basethread
from http.server import BaseHTTPRequestHandler,HTTPServer
import urllib.parse
import io

class WorkShopCtrl(BaseHTTPRequestHandler):

    def do_GET(self):
        self.send_response(200)
        self.end_headers()
        self.wfile.write("hello db".encode("utf-8"))

class CtrlServer(vavava.basethread.BaseThread):

    def __init__(self,ws,port=55555):
        vavava.basethread.BaseThread.__init__(self,"httpServer")
        self.ws = ws
        self.ser_addr = ("127.0.0.1",port)
        #self.handle = WorkShopCtrl(ws)
        self.httpd = HTTPServer( ("",port), WorkShopCtrl)

    def running_stop(self):
        self.httpd.shutdown()
        super().running_stop()

    def run(self):
        print("server on")
        while self.IsRunning:
            self.httpd.serve_forever() #.handle_request()
        print("server off")



if __name__ == "__main__":

    import  time

    try:
        ser = CtrlServer(None)
        ser.running_start()

        while True:
            time.sleep(2)

    except(KeyboardInterrupt):
        print('main thread(%f):stop'%time.clock())
        ser.running_stop()
        print('main thread(%f):stopped'%time.clock())



